package rest.jbpm.services;

import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.jbpm.api.history.HistoryTaskQuery;
import org.jbpm.pvm.internal.history.model.HistoryProcessInstanceImpl;
import org.slf4j.*;
import rest.jbpm.JbpmService;
import rest.jbpm.ProcessEngineException;
import rest.jbpm.history.RecentProcessesByUserCmd;
import rest.jbpm.wrappers.HistoryProcess;
import rest.jbpm.wrappers.lists.*;


@Path("history")
public class HistoryService extends JbpmService {

    private static final Logger log = LoggerFactory.getLogger(HistoryService.class);
    //public static final int RECENT_PROCESS_LIMIT = 30;
	@Context UriInfo uriInfo;
    public static final String PROCESS_UUID_VARIABLE_NAME = "uid";

    @GET
	@Path("avgDurationPerActivity/definition/{definitionId}")
	@Produces("text/xml")
    public DurationList getAvgDurationPerActivity(
            @PathParam("definitionId") String definitionId) {
        
        if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");

        
        return new DurationList(processEngine.getHistoryService()
                .avgDurationPerActivity(definitionId));
    }

    @GET
	@Path("choiceDistribution/definition/{definitionId}/activity/{activityName}")
	@Produces("text/xml")
    public ChoiceList choiceDistribution(
            @PathParam("definitionId") String definitionId,
            @PathParam("activityName") String activityName) {

        if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");
        
        return new ChoiceList(processEngine.getHistoryService()
                .choiceDistribution(definitionId, activityName));
    }
    
    @GET
	@Path("recentTasks/user/{userId}/limits/from/{from}/to/{to}")
    public HistoryTaskList recentTasksLimitedInterval(
            @PathParam("userId") String userId,
            @PathParam("from") int from,
            @PathParam("to") int to) {
        
        if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");

        userId = DNC14N.c14n(userId);

        return new HistoryTaskList(
                processEngine.getHistoryService().createHistoryTaskQuery()
                    .orderDesc(HistoryTaskQuery.PROPERTY_ENDTIME)
                    .page(from, to).assignee(userId).list(),
                uriInfo.getBaseUri().toString());
    }

    @GET
	@Path("recentTasks/user/{userId}/limits/{limits}")
    public HistoryTaskList recentTasksLimited(
            @PathParam("userId") String userId,
            @PathParam("limits") int limits) {
        
        if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");

        userId = DNC14N.c14n(userId);

        return new HistoryTaskList(
                processEngine.getHistoryService().createHistoryTaskQuery()
                    .orderDesc(HistoryTaskQuery.PROPERTY_ENDTIME)
                    .page(0, limits).assignee(userId).list(),
                uriInfo.getBaseUri().toString());
    }
    
    @GET
	@Path("recent/user/{userId}/limits/{limits}")
    public HistoryProcessList recentProcessesLimited(
            @PathParam("userId") String userId, @PathParam("limits") int limits) {

        if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");
        
        userId = DNC14N.c14n(userId);
        
        Collection<HistoryProcess> hps = new HashSet<HistoryProcess>();
        
        List<HistoryProcessInstanceImpl> hpiList = processEngine.execute(new RecentProcessesByUserCmd(userId, limits));
        
        for(HistoryProcessInstanceImpl hpi : hpiList)
        	hps.add(new HistoryProcess(hpi,new HistoryVariableList(hpi.getHistoryVariables())));
        
        return new HistoryProcessList(hps);
    }

}
